package com.ys.malini.dao.impl;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.ys.malini.dao.IProductDao;
import com.ys.malini.model.TImages;
import com.ys.malini.model.TProduct;
import com.ys.malini.model.TProductDetail;
import com.ys.malini.web.form.ProductForm;

/**
 * 一般产品DAO
 * 
 * @author Administrator
 * 
 */
public class ProductDao extends HibernateDaoSupport implements IProductDao {

	public ProductDao() {

	}

	/**
	 * 产品信息查询
	 * 
	 */
	public List<ProductForm> query(ProductForm form, boolean isPager) {
		System.out.println(".................");
		String bclasstype = form.getBigClassType();
		String mclasstype = form.getMidClassType();
		String sclasstype = form.getSmaClassType();
		String hsql = " from TProduct p,TImages i where p.productId=i.productId and p.productType='1' and p.bigClassType='"
				+ bclasstype + "' and p.midClassType ='" + mclasstype + "' group by i.productId";

		ProductForm product = null;
		List<ProductForm> pdlist = new ArrayList<ProductForm>();
		List<Object[]> list = getHibernateTemplate().find(hsql.toString());
		TProduct p = null;
		TImages ii = null;
		if (!list.isEmpty() && list != null) {

			for (int i = 0; i < list.size(); i++) {
				product = new ProductForm();
				p = (TProduct) (((Object[]) list.get(i))[0]);
				ii = (TImages) (((Object[]) list.get(i))[1]);
				product.setProductId(p.getProductId() + "");
				product.setProductName(p.getProductName());
				product.setBigimages(ii.getBigimages());
				product.setColor(p.getColor());
				product.setSeries(p.getSeries());
				// product.setProductInfo(p.getp)
				pdlist.add(product);

			}

		}

		return pdlist;
	}

	/**
	 * 查询 单个新品 的图片库
	 */
	public List<TImages> queryDetailImagesById(String pid) {
		StringBuffer sb = new StringBuffer(
				" from TImages i where i.productId='" + pid + "'");
		// List<TImages> list =getHibernateTemplate().find(sb.toString());
		Query query = getSession().createQuery(sb.toString());
		query.setFirstResult(0);
		query.setMaxResults(4);
		List<TImages> list = query.list();

		return list;
	}

	/**
	 * 查看单个产品
	 * 
	 */
	public ProductForm queryDetailById(String pid) {
		String hsql = " from TProduct t,TProductDetail d,TImages i  where t.productId=d.productId and i.productId=t.productId and t.productId='"
				+ pid + "'  group by i.productId ";
		System.out.println("hsql====" + hsql);
		ProductForm productdetail = null;
		List<Object[]> list = getHibernateTemplate().find(hsql);
		TProduct tp = null;
		TProductDetail td = null;
		TImages ti =null;
		if (!list.isEmpty() && list != null) {
			productdetail = new ProductForm();
			tp = (TProduct) (((Object[]) list.get(0))[0]);
			td = (TProductDetail) (((Object[]) list.get(0))[1]);
			ti =(TImages) (((Object[]) list.get(0))[2]);
			productdetail.setProductId(tp.getProductId() + "");
			productdetail.setProductName(tp.getProductName());
			productdetail.setProductType(tp.getProductType());
			productdetail.setProductStatus(tp.getProductStatus());
			productdetail.setColor(tp.getColor());
			productdetail.setBrand(tp.getBrand());
			productdetail.setTitle(tp.getTitle());
			productdetail.setSeries(tp.getSeries());
			productdetail.setMaterial(tp.getMaterial());
			productdetail.setBigClassType(tp.getBigClassType());

			productdetail.setProductCode(td.getProductCode());
			productdetail.setProductBarCode(td.getProductBarCode());
			productdetail.setProductBriefCode(td.getProductBriefCode());
			productdetail.setAreaCode(td.getAreaCode());
			productdetail.setPyear(td.getPyear());
			productdetail.setSaleDate(td.getSaleDate());
			productdetail.setRank(td.getRank());
			productdetail.setParantProdId(td.getParantProdId());
			productdetail.setProductInfo(td.getProductInfo());
			productdetail.setParameterInfo(td.getParameterInfo());
			productdetail.setPackInfo(td.getPackInfo());
			productdetail.setOtherInfo(td.getOtherInfo());
			productdetail.setMarketPrice(td.getMarketPrice());
			productdetail.setSalePrice(td.getSalePrice());
			productdetail.setBarginPrice(td.getBarginPrice());
			productdetail.setStockNum(td.getStockNum());
			productdetail.setSaleNum(td.getSaleNum());
			productdetail.setProduceDate(td.getProduceDate());
			productdetail.setSize(td.getSize());
			productdetail.setSpecCode(td.getSpecCode());
			productdetail.setColorArray(td.getColorArray());
			
			productdetail.setBigimages(ti.getBigimages());

		}

		return productdetail;
	}

	/***
	 * 
	 * 相关产品查询
	 */

	public List<ProductForm> queryRelProdById(ProductForm form) {
		System.out.println(".................");
		String series = form.getSeries();
		String pid = form.getProductId();
		String mclasstype = form.getMidClassType();
		String sclasstype = form.getSmaClassType();
		String hsql = " from TProduct p,TImages i where p.productId=i.productId and p.productType='1' and p.series='"
				+ series + "' and p.productId='" + pid + "' group by i.productId ";
		
		ProductForm product = null;
		List<ProductForm> pdlist = new ArrayList<ProductForm>();

		Query query = getSession().createQuery(hsql);
		query.setFirstResult(0);
		query.setMaxResults(4);
		List<Object[]> list = query.list();

		TProduct p = null;
		TImages ii = null;
		if (!list.isEmpty() && list != null) {

			for (int i = 0; i < list.size(); i++) {
				product = new ProductForm();
				p = (TProduct) (((Object[]) list.get(i))[0]);
				ii = (TImages) (((Object[]) list.get(i))[1]);
				product.setProductId(p.getProductId() + "");
				product.setProductName(p.getProductName());
				product.setBigimages(ii.getBigimages());
				product.setColor(p.getColor());
				// product.setProductInfo(p.getp)
				pdlist.add(product);

			}

		}

		return pdlist;
	}

	public TProduct queryById(String productId) {
		// TODO Auto-generated method stub
		return null;
	}

}
